package com.kaishengit.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.kaishengit.pojo.Product;
import com.kaishengit.util.HibernateUtil;

public class ProductDao {

	public List<Product> findAll() {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery("from Product");
		List<Product> products = query.list();
		session.beginTransaction().commit();
		return products;
	}

	public void save(Product product) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		session.persist(product);
		session.beginTransaction().commit();
	}

	public Product findById(int id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Product product = (Product) session.get(Product.class, id);
		session.beginTransaction().commit();
		return product;
	}

	public void update(Product product) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		session.merge(product);
		session.beginTransaction().commit();
	}

	public void delete(int id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Product product = (Product) session.get(Product.class, id);
		session.delete(product);
		session.beginTransaction().commit();

	}
}
